/* Be sure server is running. */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>

#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#include <kvsp.h>

int main(int argc, char** argv)
{
	int sockfd, op;
	char key[1024];
	memset(key, 0, sizeof(key));
	strcpy(key, "key");

	struct sockaddr_in servaddr;
        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(8080); /* Specify Server Listen Port. */
        inet_pton(AF_INET, "127.0.0.1", &(servaddr.sin_addr)); /*Specify Server IP */


	/* Connect to server */
        sockfd = conn_serv(&servaddr);  


	/* PUT to Server */
	char* value=malloc(1024);
	strcpy(value, "valuevalue");
	int value_size=strlen(value);
        op=data_trans(PUT, sockfd, key, strlen(key), &value, &value_size);
	if (!op)
	{
		printf ("PUT success.\n");

	}
	else
	{
		printf ("PUT failed.\n");
	}
	free(value);


	/* GET from server, needn't malloc a buffer, protocol do it */
	char *buffer=NULL;
	int buffer_size;
        op=data_trans(GET, sockfd, key, strlen(key), &buffer, &buffer_size);
	if (!op)
	{
		printf ("GET success.\n");
	}
	else
	{
		printf ("GET failed.\n");
	}
	printf ("[SUCCESS][%s] get buffer size:%d, value: %s\n",key, buffer_size, buffer);
	free(buffer);  /* malloc at protocol.c */

	return 0;
}


